var module = require('./lib/module.Base');
$(document).ready(function($) {
    var HTML_template = {
        purchaseList:'<div class="buttons-tab dbuttons-tab2">' +
        '<a href="#tabW" class="tab-link active button">待采购（<%= _.toLength(_.result(_.groupBy(data,\'orderStatus\'),\'WAITING_FOR_PURCHASE\')) %>）</a>' +
        '<a href="#tabY" class="tab-link button">已采购</a>' +
        '</div>' +
        '<div class="content-block margin0 padding0"><div class="tabs">' +
        '<% _.forEach({W:_.groupBy(data,\'orderStatus\')[\'WAITING_FOR_PURCHASE\'],Y:_.groupBy(data,\'orderStatus\')[\'YFK\']}, function(purchase,purchaseType) { %>' +
        '<div id="tab<%=purchaseType%>" class="tab <%if(_.eq(purchaseType,\'W\')){%>active<%}%>">' +
        '<% _.forEach(purchase, function(list,key) { %>' +
        '<div class="bgf mb5h"><div class="content-padded pt5h" style="margin: 0 .5rem"><div class="row no-gutter borderB pb5h"><div class="col-50 c9"><p class="margin0 c3 fs75"><%=list.productName%></p></div><div class="col-50 c3 tar"><%=list.quantity%>箱</div></div><div class="row no-gutter pt5h pb5h">' +
        '<div class="col-30 c9"><%= list.completeTime %></div>' +
        '<div class="col-15 c9">&nbsp;</div>' +
        '<%if(_.eq(purchaseType,\'Y\')){%><div class="col-15 c3 tar">&nbsp;</div>' +
        '<div class="col-5 c9">&nbsp;</div><%}%>' +
        '<div class="col-15 c3 tar"><a href="javascript:void(0);" class="button button-fill h-null-btn btn-copy">复制</a></div>' +
        '<div class="col-5 c9">&nbsp;</div>' +
        '<div class="col-15 c3 tar"><a href="javascript:void(0);" class="button button-fill h-null-btn btn-zf">转发</a></div>' +
        '<%if(_.eq(purchaseType,\'W\')){%><div class="col-5 c9">&nbsp;</div>' +
        '<div class="col-15 c3 tar"><a href="javascript:void(0);" data-purchase-id="<%=list.id%>" class="button button-fill red-null-btn prompt-title-ok">采购</a></div><%}%>' +
        '</div></div></div>' +
        '<%})%>' +
        '</div><%})%></div></div>',
        inputTemp:'<% _.forEach(data, function(item,key) { %><div class="content-padded fs75 "><div class="row no-gutter"><div class="col-45" style="width: 45%; overflow: hidden;"><%=item.productName%></div><div class="col-20"><p class="margin0 tar c6">单价:</p></div><div class="col-25"><p class="margin0 tac c3"><input type="text" class="c3 bgn wall quantity-input" value="<%=item.price%>" name="<%=item.id%>"></p></div><div class="col-10">元</div></div></div><%})%>',
        success:'<p class="margin0 c9 tar fs75">已采购</p>'
    };

    var localData = {
        purchaseList:[]
    };
    _.extend(module.PageEventBase.prototype,{
        purchase:function ($page,pageId,result) {
            if(result.status){
                localData.purchaseList = result.data.list;
            }
            $.loadTemplate($page, {
                purchaseList:{data:_.chain(localData.purchaseList).map(function (o) {
                    o.completeTime = moment(o.completeTime).format('YYYY-MM-DD');
                    return o;
                }).value()}
            },HTML_template).then(function(){
                var modalItems = [];
                $page.find('.prompt-title-ok').on('click',function () {
                    var btnDom = $(this);
                    var ids = _.split($(this).data('purchaseId'),',').map(_.toInteger);
                    var modalItems = _.filter(localData.purchaseList,function (o) {
                        return _.indexOf(ids,o.id) >= 0;
                    });
                    var templateModal = _.template(HTML_template.inputTemp);
                    $.modal({
                        title:  '请输入商品单价:',
                        text: templateModal({data:modalItems}),
                        buttons: [
                            {
                                text: '取消',
                                onClick: function() {

                                }
                            },
                            {
                                text: '确认',
                                onClick: function(e) {
                                    var changeData = _.chain($(e).find('input')).map(function (input) {
                                        return {
                                            id:_.toInteger($(input).attr('name')),
                                            price:_.floor($(input).val(),2),
                                            isError:/^\d+(\.\d+)?$/.test($(input).val())
                                        }
                                    }).value();
                                    if(_.filter(changeData,{isError:false}).length > 0){
                                        $.alert('请输入正确的产品单价');
                                    }else{
                                        modalItems = modalItems.map(function (item) {
                                            return _.findIndex(changeData,{id:item.id}) >= 0 ? _.assign(item,_.find(changeData,{id:item.id})) : item;
                                        })[0];
                                        //提交事件
                                        $.alert('采购成功',function () {
                                            btnDom.parent().append(HTML_template.success);
                                            btnDom.remove();
                                        });
                                    }


                                }
                            }
                        ]
                    });

                });
            });
        }
    });
    var EventList = new module.PageEventBase();
    EventList.setResolve({
        purchase:{status:true,url:'/app/purchaseOrders.htm',params:{}}
    });
    var pageId = $('.page.page-current').attr('id');
    EventList.init($('.page.page-current'),pageId);
});